<template>
    <div class="post" ref="post">
        <div class="cover"></div>
        <div class="wrap">
          <div class="tit">
               <div class="account"><div class="text">标题</div><input type="text" v-model="title"></div>
                 <div class="password"><div class="text">简介</div><input type="text" v-model="introduce"></div>
              </div>
        </div>
     
        <div class="footer">
          <div class="head">
                        <div class="logo"><span>编辑 </span><span>文章</span></div>
                        <p>好好学习，天天敲代码~</p>
                    </div>
            <div class="edifyer">
              <div class="tool">
                <a href="https://img.tg/yoren" class="el-icon-picture" target="_blank"></a>
              </div>
              <div class="markdown-editor">
              <div class="editor">
                <textarea v-model="markdownContent" @input="updatePreview"></textarea>
              </div>
        
              <div class="preview" v-html="htmlContent"></div>
            </div>
            </div>
           <div class="button">
              <div class="loginButton" @click="edify">修改</div>
              <div class="back" @click="back">取消</div>
          </div>
        </div>
        </div>
</template>

<script>
import MarkdownIt from 'markdown-it'
import hljs from 'highlight.js'; // 引入 highlight.js
import markdownItHighlightJs from 'markdown-it-highlightjs'; // 引入 markdown-it-highlightjs
// import { postArticles } from '@/api/article'
import alert from '@mdit/plugin-alert'
import { mark } from '@mdit/plugin-mark'
import { attrs } from '@mdit/plugin-attrs'
import { edify } from '@/api/article';


export default {
    data() {
        return {
            title: '',
            introduce: '',
            markdownContent: '',
            htmlContent: ''
        };
    },
    methods: {
        back() {
            this.$router.go(-1)
        },
       async edify(){


        this.$confirm('确定要修改文章吗?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(async () => {
                const list = {
                    aId: this.$route.query.aId,
                    title: this.title,
                    introduce: this.introduce,
                    text: this.htmlContent,
                    mdText:this.markdownContent
                }
                const data = await edify(list)
                if (data.code === '000') {
                    this.$message({
                        message: '修改成功啦~',
                        type: 'success',
                        duration: 1000
                    })
                    this.$router.go(-1)
                }
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消操作'
                });
            });


            // const list={
            //     aId: this.$route.query.aId,
            //     title:this.title,
            //     introduce:this.introduce,
            //     text:this.htmlContent,
            //    time: new Date(),
            // }
            // const data=await edify(list)
            // if (data.code === '000') {
            //     this.$message({
            //         message: '修改成功啦~',
            //         type: 'success',
            //         duration: 1000
            //     });
            // }
       },
        updatePreview() {
            // 使用 markdown-it 将 Markdown 文本转换为 HTML
            this.htmlContent = this.md.render(this.markdownContent);
        },
        
        
    },
    created() {

        this.title= this.$route.query.title
        this.introduce= this.$route.query.introduce
        // this.htmlContent= this.$route.query.text
        this.markdownContent= this.$route.query.mdText

        // 初始化 markdown-it 实例
        this.md = new MarkdownIt({
            html: true, // 允许渲染 HTML 标签
            linkify: true, // 自动链接某些文本为链接
            table: true,
            highlight: (str, lang) => {
                if (lang && hljs.getLanguage(lang)) {
                    try {
                        return hljs.highlight(lang, str).value;
                    } catch (err) { console.log(err) }
                }

                try {
                    return hljs.highlightAuto(str).value;
                } catch (err) { console.log(err) }

                return ''; // 使用空字符串表示未识别的语言
            }
        });
        this.md.use(markdownItHighlightJs);
        this.md.use(alert);
        this.md.use(mark);
        this.md.use(attrs);



        // 初始化时更新预览
        this.updatePreview();
    }
};
</script>

<style src="@/assets/css/sspai.less" lang="less" scoped></style>
<style lang="less" scoped>
.cover {
    position: fixed;
    top: 0;
    background-color: #220F4Bd9;
    width: 100vw;
    height: 100vh;
    z-index: 1;
}

.head {
    width: 100%;
    text-align: center;

    padding-left: 30px;
    margin-bottom: 10px;

    position: relative;
    z-index: 4;
}

.head p {
    color: #fff;
    font-size: 12px;
    line-height: 30px;
}

.logo :nth-child(1) {
    color: #E3C420;
    font-weight: 900;
    font-size: 25px;
}

.logo :nth-child(2) {
    color: #FF1A89;
    font-weight: 900;
    font-size: 25px;
}

.footer {
    height: 100%;
    // padding: 0 20px;
}

.post {
    overflow-y: hidden;
    height: 100vh;
    background: url('@/assets/regBk.avif') no-repeat;
    background-size: 100%;
    //  background-color:  #220F4B; 
    padding: px;

}

.wrap {
    padding: 20px;
}

.tit {

    z-index: 4;
    position: relative;
    padding: 20px;
    /* border: 4px solid #2F00FF; */
    border-radius: 20px;
    background-color: rgba(7, 3, 30, 0.4);

}

.text {
    height: 100%;
    line-height: 40px;
    font-size: 16px;
    font-weight: bold;
    // color: #E3C420;
    color: #EC38ED;
    padding-right: 10px;

}

.account {
    width: 50%;
}

.password {
    width: 90%;
    margin-top: 20px;
}

.account,
.password {
    height: 40px;
    display: flex;
    /* border-bottom: 2px solid #2F00FF; */


}

input {
    flex: 1;
    outline: none;
    border: none;
    text-indent: 20px;
    color: #E3C420;
    background-color: transparent;
    border: 2px solid #2F00FF;
    border-radius: 10px;
    font-size: 18px;


}

.content {
    position: relative;
    z-index: 4;
    line-height: 40px;
    font-size: 17px;
    font-weight: bold;
    color: #FF1A89;
    padding-left: 20px;
}

.edifyer {
    z-index: 4;
    position: relative;
    padding: 0 30px;
}

.tool {

    height: 40px;
    background-color: #1d1629;
    line-height: 40px;
    padding: 0 20px;
    border: 4px solid black;
    border-bottom: none;

}

.tool a {
    color: #8b7f8e;
    font-size: 20px;
}

.markdown-editor {

    display: flex;
    // padding-left: 20px;
    /* padding: 0 20px; */
}

.editor,
.preview {
    /* padding: 1rem; */
    box-sizing: border-box;
    height: 500px;
    color: #fff;
}

.editor {
    width: 60%;

}

.editor textarea {
    width: 100%;
    height: 100%;
    resize: none;
    background-color: #241c35;
    // background-color:#291f3b;
    color: #8b7f8e;
    /* background-color: #251825; */
    padding: 16px;
    outline: none;
    font-size: 18px;
    border: 4px solid black;
}

.preview {
    flex: 1;
    color: #8b7f8e;
    background-color: #241c35;
    padding: 16px;
    outline: none;
    border: 4px solid black;
    overflow: auto;
}


.button {
    z-index: 4;
    position: relative;
    width: 100%;
    display: flex;
    justify-content: left;
    padding-left: 30px;
    margin-top: 30px;

}

.loginButton,
.back {
    width: 10%;
    height: 50px;
    color: black;
    text-align: center;
    line-height: 40px;
    font-size: 18px;
    font-weight: 600;
    transition: 0.5s;
    cursor: pointer;
}

.loginButton {
    background-color: #fe54c3;
    border: 5px solid #6934aa;
}

.back {
    background-color: #9a56ee;
    border: 5px solid #6934aa;
    margin-left: 20px;
}

.loginButton:hover,
.back:hover {
    background-color: #56EE8F;
    color: #f3f0f0;
}</style>